/**
 * @Title: index
 * @Package mall-fe
 * @author Shinelon
 * @date 2017/9/4 14:01
 * @version V1.0
 */
'use strict';
require('./index.css');
var shiqla = require('util/shiqlamall');
var userService = require('service/user-service');
var cartService = require('service/cart-service');

var nav = {
    init : function () {
        this.bindEvent();
        this.loadUserInfo();
        this.loadCartCount();
        return this;
    },
    bindEvent:function () {
        //登录点击事件
        $('.js-login').click(function () {
            shiqla.doLogin();
        });
        //注册点击事件
        $('.js-register').click(function () {
           window.location.href = './user-register.html';
        });
        //退出点击事件
        $('.js-logout').click(function () {
            userService.logout(function (res) {
                // window.location.reload();
                window.location.href='./index.html';
            },function (errMsg) {
                shiqla.errorTips(errMsg);
            });
        });
    },
    loadUserInfo:function () {
        userService.checkLogin(function (res) {
            $('.user.not-login').hide();
            $('.user.login').show().find('.username').text(res.username);
        },function (errMsg) {
        });
    },
    loadCartCount:function () {
        cartService.getCartCount(function (res) {
            $('.nav .cart-count').text(res || 0);
        },function (errMsg) {
            $('.nav .count-cont').text( 0);
        });
    }
};

module.exports = nav.init();